﻿#region GNU General Public License
// XEdit2D - XNA 2D Asset Editor
// Copyright (C) 2010  HurlBat Games

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;

namespace HurlBat.XEdit2D
{
    public partial class Images : UserControl, IImagesControl
    {
        IMainForm f = null;
        IMainForm Form
        {
            get
            {
                if (f == null) f = (IMainForm)this.FindForm();
                return f;
            }
        }

        public Images()
        {
            InitializeComponent();
        }


        private void batchCreateOneForEach_Click(object sender, EventArgs e)
        {
            BatchCreateAnimation batchAnimationOptions = new BatchCreateAnimation();
            if (batchAnimationOptions.ShowDialog() != DialogResult.OK) return;

            Point frameDim = new Point(
                Decimal.ToInt32(Globals.AppStore.FrameWidth),
                Decimal.ToInt32(Globals.AppStore.FrameHeight));
            Point frameNum = new Point(
                Decimal.ToInt32(Globals.AppStore.FramesPerRow),
                Decimal.ToInt32(Globals.AppStore.FramesPerColumn));
            int totalFrames = Decimal.ToInt32(Globals.AppStore.TotalFrames);

            Form.AnimationsControl.ResetImageList(70, 70);
            Form.ResetProgressBar();

            for (int i = 0; i < listImages.SelectedItems.Count; i++)
            {
                if (!Form.UsedImages.ContainsKey(listImages.SelectedItems[i].Text))
                    Form.UsedImages.Add(listImages.SelectedItems[i].Text,
                        Image.FromFile(Convert.ToString(listImages.SelectedItems[i].Tag)));
                HurlBat.Data2D.Graphics.Animation anim = new HurlBat.Data2D.Graphics.Animation(listImages.SelectedItems[i].Text);
                //Image img = Image.FromFile(Convert.ToString(i.Tag));
                Point fDim = frameDim;
                Point fNum = frameNum;
                Point iDim = new Point(Form.UsedImages[listImages.SelectedItems[i].Text].Size);

                if (fDim.X * fNum.X > iDim.X || fDim.Y * fNum.Y > iDim.Y)
                {
                    Form.Errors.Add(string.Format("{0}: image is smaller than specified",
                        listImages.SelectedItems[i].Text));
                    continue;
                }
                if (fDim.X == 0) fDim.X = iDim.X / fNum.X;
                if (fDim.Y == 0) fDim.Y = iDim.Y / fNum.Y;
                if (fNum.X == 0) fNum.X = iDim.X / fDim.X;
                if (fNum.Y == 0) fNum.Y = iDim.Y / fDim.Y;
                if (totalFrames == 0) totalFrames = fNum.X * fNum.Y;

                for (int current = 0; current < totalFrames; current++)
                {
                    anim.Frames.Add(new HurlBat.Data2D.Graphics.Image(Convert.ToString(listImages.SelectedItems[i].Tag),
                        new Microsoft.Xna.Framework.Rectangle(
                            (current % fNum.X) * fDim.X,
                            (current / fNum.X) * fDim.Y,
                            fDim.X, fDim.Y)));
                }
                Bitmap cropped = Util.Crop(Form.UsedImages[listImages.SelectedItems[i].Text],
                    new Rectangle(0, 0, fDim.X, fDim.Y));
                Form.AnimationsControl.AddAnimation(anim, cropped);

                Form.UpdateProgressBar((i * 100) / listImages.SelectedItems.Count);
            }
            foreach (ListViewItem i in listImages.SelectedItems)
                listImages.Items.Remove(i);
            listImages.SelectedItems.Clear();
            Form.SwitchTab(TabType.Animations);

            Form.HideProgressBar();
        }

        private void listImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool toEnable = listImages.Items.Count > 0 && listImages.SelectedItems.Count > 0;
            toolRemoveImages.Enabled = toEnable;
            toolCreateAnimation.Enabled = toEnable;
            //toolStatusLabelLeft.Text = string.Format("{0} item(s) selected out of {1}",
            //    listImages.SelectedItems.Count, listImages.Items.Count);
        }

        private void toolAddImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Multiselect = true;
            DialogResult res = op.ShowDialog();
            if (res != DialogResult.OK) return;

            Form.ResetProgressBar();

            listImages.LargeImageList = new ImageList();
            listImages.LargeImageList.ImageSize = new Size(70, 70);
            for (int i = 0; i < op.FileNames.Length; i++)
            {
                Bitmap img = Util.MakeSquare(Image.FromFile(op.FileNames[i]));
                img.Tag = op.FileNames[i];
                listImages.LargeImageList.Images.Add(img);
                ListViewItem item = new ListViewItem(op.SafeFileNames[i], listImages.LargeImageList.Images.Count - 1);
                item.Tag = op.FileNames[i];
                listImages.Items.Add(item);
                Form.UpdateProgressBar((i * 100) / op.FileNames.Length);
            }
            Form.HideProgressBar();
        }

        private void toolRemoveImages_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(
                string.Format("Are you sure you want to remove {0} images ?", listImages.SelectedItems.Count),
                "Confirm delete", MessageBoxButtons.YesNo) != DialogResult.Yes)
                return;

            foreach (ListViewItem i in listImages.SelectedItems)
                listImages.Items.Remove(i);
            listImages.SelectedItems.Clear();
        }

        private void toolSelectAllImages_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listImages.Items.Count; i++)
                listImages.Items[i].Selected = true;
        }

    }
}
